<?php
namespace zhtfrontend\modules\cus\components\services;

use common\components\helpers\Fun;
use common\models\datas\Customer;
use common\models\datas\Customergrade;
use common\models\datas\Student;
use yii\base\Component;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

class Cus extends Component
{
     public static function GetCustomerList($where=[],$page=1,$pageSize = 12){
             $site_id = Fun::getSiteId();
             $model = Customer::find()->where(['site_id'=>$site_id]);
             if (!empty($where['tel'])) {
                 $model->andWhere(['mobile' => $where['tel']]);//tel字段改为mobile
             }
             if (!empty($where['name'])) {
                 $model->andWhere(['name' => $where['name']]);
             }
             if (!empty($where['customer_status'])) {
                 $model->andWhere(['grade_id' => $where['customer_status']]);
             }
             if(!empty($where['sort'])){
                  $model->orderBy($where['sort'].'DESC');
             }else{
                 $model->orderBy('id DESC');
             }
             $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => $pageSize,'params'=>$where]);
             isset($page) ? $pages->page = $page - 1 : 0;
             if (isset(\Yii::$app->user->identity) && in_array(\Yii::$app->user->identity->role, ['admin', 'administrator'])) {
                 $data = $model->offset($pages->offset)->limit($pages->limit)->all();
             } else {
                 if (!empty($where['tel']) || !empty($where['name']) || !empty($where['card_id']))
                     $data = $model->all();
             }
             $data = Fun::asArray($data, ['name', 'age', 'sex', 'mobile', 'card_id', 'operate', 'grade', 'account', 'is_sender', 'email', 'integral']);
         return ['data'=>$data,'pages'=>$pages];
     }

     public static function InsertCustomer($data,$extend= []){
         $data['site_id'] = Fun::getSiteId();
         $data['created_at'] = time();
         if(!empty($extend)){
             foreach ($extend as $key=>$val){
                 $data[$key]=$val;
             }
         }
           $res = Fun::getSysDb()->createCommand()->insert("{{%customer}}",$data)->execute();
           return $res;
     }
     public static function UpdateCustomer($data,$id= 0,$extend= []){
         if(!empty($extend)){
             foreach ($extend as $key=>$val){
                 $data[$key]=$val;
             }
         }
         $res = Fun::getSysDb()->createCommand()->update("{{%customer}}",$data,"id=:id",['id'=>$id])->execute();
         return $res;
     }
     public static function GetGradeList(){
         $grade = Customergrade::find()->where(['is_delete' => 0])->all();
         return $grade;
     }

     public static function GetGrade(){
         $model = Customergrade::find()->where(['is_delete'=>0])->all();
         return $model;
     }

     public static function GetConfig($module='customer',$data=[]){
         $extend = Cusfc::GetDataList($module);
         $str = '';
         foreach ($extend as $item){
             $value = isset($data[$item['code']])?$data[$item['code']]:null;
             $required = $item['is_required'] == 1?'extend_required':'';
              $str .= '<tr>';
              $str .= '<th class="zx-goods-table">'.$item["notes"].'</th>';
              $str .= '<td><div class="form-group">';
              if($item['type'] == 'input'){
                 $str .= Html::input('text',"extend[{$item['code']}]",$value,['class'=>'input-text '.$required,'id'=>$item['code']]);
              }elseif ($item['type'] == 'select'){
                 $arr = explode(' ',$item['type_value']);
                  $arrs = [];
                 foreach ($arr as $val){
                     $arrs[$val] = $val;
                 }
                  $str .= Html::dropDownList("extend[{$item['code']}]",$value,$arrs,['class'=>'input-dropdownlist '.$required,'id'=>$item['code']]);
              }else{
                  $str .= Html::textarea("extend[{$item['code']}]",$value,['class'=>'input-textarea '.$required,'id'=>$item['code']]);
              }
              $str .= Html::tag('div','',['class'=>'help-block']);
              $str .= '</div></td>';
              $str .= '</tr>';
         }
         return $str;
     }

     public static function GetInfo($id){
        return Customer::findOne($id);
     }

     public static function GetStudentList($customer_id = 0){
         $studentList = Student::find()->where(['customer_id'=>$customer_id])->all();
         return $studentList;
     }

    public static function InsertStudent($data,$extend= []){
        $data['created_at'] = time();
        if(!empty($extend)){
            foreach ($extend as $key=>$val){
                $data[$key]=$val;
            }
        }
        \Yii::$app->db->createCommand()->insert("{{%student}}",$data)->execute();
        $id = \Yii::$app->db->getLastInsertID();
        return $id;
    }

    public static function UpdateStudent($data,$id = 0,$extend= []){
        $data['updated_at'] = time();
        if(!empty($extend)){
            foreach ($extend as $key=>$val){
                $data[$key]=$val;
            }
        }
        $res = Fun::getSysDb()->createCommand()->update("{{%student}}",$data,"id=:id",['id'=>$id])->execute();
        return $res;
    }

    public static function getBirthday($idcard) {
            if(empty($idcard)) return null;
            $bir = substr($idcard, 6, 8);
            $year = (int) substr($bir, 0, 4);
            $month = (int) substr($bir, 4, 2);
            $day = (int) substr($bir, 6, 2);
            $data =  [
                'birthday'=>$year . "-" . $month . "-" . $day,
                'year' =>$year
            ];
            return $data;
    }

    public static function GetStudentInfo($id){
         return Student::findOne($id);
    }
}